import {Injectable} from '@angular/core';
import {ActionSheetController, AlertController, Loading, LoadingController, ToastController} from "ionic-angular";

/*
  Generated class for the DialogProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class DialogProvider {
  private loading: Loading;
  private loadingIsOpen: boolean = false;

  constructor(
    private actionsheetCtrl: ActionSheetController,
    private alertCtrl: AlertController,
    private toastCtrl: ToastController,
    private loadingCtrl: LoadingController
  ) {
  }

  //向上弹出层
  showActionSheet(typesOfComplaints: any[], title?: string, cancelBut?: boolean) {
    let pTitle = title || '暂无数据';
    let actionSheet = this.actionsheetCtrl.create({
      title: pTitle,
      cssClass: 'action-sheets-basic-page',
      buttons: typesOfComplaints,
      enableBackdropDismiss: false
    });
    if (cancelBut != false) {
      actionSheet.addButton({
        text: '取消',
        role: 'cancel'
      });
    }

    actionSheet.present();
  }

  //弹出提示
  showAlert(subTitle, title?: string, okfun?: () => void): any {
    let pTitle = title || '提示';
    let alert = this.alertCtrl.create({
      title: pTitle,
      subTitle: subTitle,
      mode: "ios",
      buttons: [{
        text: '确定',
        handler: () => {
          if (okfun) okfun();
        }
      }]
    });
    alert.present();
    return alert;
  }

  /**
   * 弹出确认取消提示框
   * @param okfun 选择确认要执行的回调方法
   * @param subTitle 提示文本
   * @param title 选填提示框标题
   * @param notfun 选择取消要执行的回调方法
   */
  showConfirm(okfun, subTitle, title?: string, notfun?): any {
    let pTitle = title || '提示';
    let confirm = this.alertCtrl.create({
      title: pTitle,
      message: subTitle,
      buttons: [
        {
          text: '取消',
          handler: () => {
            if (notfun) notfun();
          }
        },
        {
          text: '确定',
          handler: () => {
            okfun();
          }
        }
      ]
    });
    confirm.present();

    return confirm;
  }

  /**
   * 弹出单选或多选框
   * @param okFun 选择确认要执行的回调方法
   * @param pSubTitle 提示文本
   * @param pInputs 选择条目的配置
   * @param notfun 选择取消要执行的回调方法
   */
  showRadioOrCheckbox(okFun: Function, pSubTitle: string, pInputs: any[],notfun?) {
    let alert = this.alertCtrl.create({
      message: pSubTitle,
      inputs: pInputs,
      buttons: [
        {
          text: '取消',
          handler: () => {
            if (notfun) notfun();
          }
        },
        {
          text: '确定',
          handler: data => {
            okFun(data);
          }
        }
      ]
    });
    alert.present();
  }

  /**
   * 统一调用此方法显示提示信息
   * @param message 信息内容
   * @param duration 显示时长
   */
  showToast(message?: string, duration?: number, position?: string): void {
    let pMessage = message || '操作成功';
    let pDuration = duration || 3000;
    let pPosition = position || 'top';
    this.toastCtrl.create({
      message: pMessage,
      duration: pDuration,
      position: pPosition,
      showCloseButton: true,
      closeButtonText: '关闭'
    }).present();
  };

  /**
   * 弹出输入框
   * @param parameter
   */
  showPrompt(parameter: { 'title'?: string, 'message'?: string, 'inputs': any[], 'cancelFn'?: (data?) => void, 'doneFn'?: (data?) => void }) {
    let alert = this.alertCtrl.create({
      title: parameter['title'] || '',
      message: parameter['message'] || '',
      inputs: parameter['inputs'],
      buttons: [
        {
          text: '取消',
          handler: parameter['cancelFn']
        },
        {
          text: '确定',
          handler: parameter['doneFn']
        }
      ]
    });
    alert.present();
  }

  /**
   * 统一调用此方法显示loading
   * @param content 显示的内容
   */
  showLoading(content: string = '', duration: number = 100000): void {
    if (!this.loadingIsOpen) {
      this.loadingIsOpen = true;
      this.loading = this.loadingCtrl.create({
        content: content
      });
      this.loading.present();
      setTimeout(() => {//最长显示100秒
        this.loadingIsOpen && this.loading.dismiss();
        this.loadingIsOpen = false;
      }, duration);
    }
  };

  /**
   * 关闭loading
   */
  hideLoading(): void {
    this.loadingIsOpen && this.loading.dismiss();
    this.loadingIsOpen = false;
  };
}
